package com.javalong.mir.bean;

import java.util.ArrayList;
import java.util.HashMap;

import org.sword.wechat4j.message.TemplateMsg;
import org.sword.wechat4j.message.template.TemplateMsgBody;
import org.sword.wechat4j.message.template.TemplateMsgData;

/**
 * 消息内容类
 * @author Junyan2015
 * @date 2016年4月18日 上午11:14:07
 * @since
 */
public class TemplatMsgObject extends HashMap<String, String> {

	
	private TemplateMsg msg;
	
	private TemplateMsgBody body;

	public TemplatMsgObject()
	{
		body=new TemplateMsgBody();
		body.setData(new ArrayList<TemplateMsgData>());
		msg=new TemplateMsg();
	}
	
	public TemplatMsgObject(String accessToken)
	{
		body=new TemplateMsgBody();
		body.setData(new ArrayList<TemplateMsgData>());
		msg=new TemplateMsg(accessToken);
	}
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	//大部分模板使用first字段，其余在取时判断
	public void setFirstData(String value) {
		addParam("first", value);
	}
	//大部分模板使用remark字段，其余在取时判断
	public void setRemarkData(String value) {
		addParam("remark", value);
	}
	
	public TemplateMsgBody getBody() {
		return body;
	}
	
	public void setOpenId(String openId)
	{
		body.setTouser(openId);
	}
	
	public void setTemplateId(String templateId)
	{
		body.setTemplateId(templateId);
	}
	
	public void setUrl(String url)
	{
		body.setUrl(url);
	}
	
	public void setFirst(String first)
	{
		addParam("first",first);
	}
	
	public void setRemark(String remark)
	{
		addParam("remark",remark);
	}
	
	//添加真正信息,可添加多个
	public void addParam(String key, String value) {
		
		TemplateMsgData templateMsgData = new TemplateMsgData();
		templateMsgData.setName(key);
		templateMsgData.setValue(value);
		
		body.getData().add(templateMsgData);
	}
	
	public void send()
	{
		String msgId = msg.send(body);
		if(msgId != null) {
			//logger.info("消息发送成功！wechat发送消息ID为"+msgId);
		}else {
			//logger.info("消息发送失败!");
		}
	}
}
